package com.chs.recoginition.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chs.recoginition.core.utils.BeanMapper;
import com.chs.recoginition.mapper.IdentificationContentMapper;
import com.chs.recoginition.pojo.IdentificationContent;
import com.chs.recoginition.service.IIdentificationContentService;
import com.chs.recoginition.vo.PageRespVo;
import com.chs.recoginition.vo.reqvo.IdentificationPageVo;
import com.chs.recoginition.vo.respVo.ImageRecognitionRespVo;
import com.chs.recoginition.vo.respVo.UserRespVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 识别内容 服务实现类
 * </p>
 * @author CHS
 * @since 2023-04-08
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class IdentificationContentServiceImpl extends ServiceImpl<IdentificationContentMapper, IdentificationContent> implements IIdentificationContentService {

    @Override
    public PageRespVo<ImageRecognitionRespVo> findPage(IdentificationPageVo reqVo) {
        PageRespVo<ImageRecognitionRespVo> result = baseMapper.findPage(reqVo);
        for(int i=0;i<result.getRows().size();i++) {
            String id = result.getRows().get(i).getId();
            QueryWrapper<IdentificationContent> queryWrap = new QueryWrapper<>();
            queryWrap.eq("PARENT_ID",id);
            List<IdentificationContent> list = baseMapper.selectList(queryWrap);
            result.getRows().get(i).setChildRespVos(list);
        }
        return result;
    }

    @Override
    public List<IdentificationContent> findListByParentId(String parentId) {
        QueryWrapper<IdentificationContent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PARENT_ID",parentId);
        List<IdentificationContent> identificationContents = baseMapper.selectList(queryWrapper);
        return identificationContents;
    }

    @Override
    public ImageRecognitionRespVo findById(String id) {
        IdentificationContent content = baseMapper.selectById(id);
        if(content == null) {
            return null;
        }
        ImageRecognitionRespVo result = BeanMapper.map(content, ImageRecognitionRespVo.class);
        QueryWrapper<IdentificationContent> queryWrap = new QueryWrapper<>();
        queryWrap.eq("PARENT_ID",id);
        List<IdentificationContent> list = baseMapper.selectList(queryWrap);
        result.setChildRespVos(list);
        return result;
    }

    @Override
    public String save(ImageRecognitionRespVo vo, UserRespVo user) {
        IdentificationContent identificationContent = new IdentificationContent();
        String id = vo.getId();
        IdentificationContent content = baseMapper.selectById(id);
        if(content != null) {
            if(StringUtils.isNotBlank(vo.getName())) {
                content.setName(vo.getName());
                baseMapper.updateById(content);
            }
            return id;
        }
        identificationContent.setId(vo.getId());
        identificationContent.setName(vo.getName());
        identificationContent.setImagePath(vo.getImagePath());
        identificationContent.setCreatedBy(user.getUserId());
        identificationContent.setCreatedTime(LocalDateTime.now());
        //记录父内容ID
        String result = identificationContent.getId();
        List<IdentificationContent> childRespVos = vo.getChildRespVos();
        childRespVos.forEach(v->{
            if(v.getId() == null) {
                v.setId(IdWorker.get32UUID());
            }
            v.setParentId(identificationContent.getId());
            v.setCreatedBy(user.getUserId());
            v.setCreatedTime(LocalDateTime.now());
        });
        childRespVos.add(identificationContent);
        this.saveBatch(childRespVos);
        return result;
    }

    @Override
    public void saveInfo(IdentificationContent vo, UserRespVo user) {
        vo.setCreatedBy(user.getUserId());
        vo.setCreatedTime(LocalDateTime.now());
        baseMapper.insert(vo);
    }

    @Override
    public void modify(IdentificationContent vo) {
        if(vo.getId() == null) {
            return;
        }
        baseMapper.updateById(vo);
    }

    @Override
    public void remove(String idList) {
        List<String> ids = Arrays.stream(idList.split(",")).map(String::toString).collect(Collectors.toList());
        //删除子内容
        for(String id : ids) {
            QueryWrapper<IdentificationContent> queryWrap = new QueryWrapper<>();
            queryWrap.eq("PARENT_ID",id);
            List<IdentificationContent> list = baseMapper.selectList(queryWrap);
            if(!list.isEmpty()) {
                List<String> childIds = list.stream().map(IdentificationContent::getId).collect(Collectors.toList());
                this.removeByIds(childIds);
            }
        }
        this.removeByIds(ids);
    }
}
